package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.payconfig.EntityRestStaff;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.staff.GetRestStaffListRequest;
import com.ocom.common.request.payconfig.staff.RemoveRestStaffRequest;
import com.ocom.common.utils.CommonUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoRestStaff;
import com.ocom.payconfig.interceptor.ScopeQueryWrapper;
import com.ocom.payconfig.service.RestStaffService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RestStaffServiceImpl extends ServiceImpl<IDaoRestStaff, EntityRestStaff> implements RestStaffService {


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public PageInfo<EntityRestStaff> getList(Long companyId, GetRestStaffListRequest request) {
        ScopeQueryWrapper<EntityRestStaff> queryWrapper = new ScopeQueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", request.getMerchId());

        if(!CommonUtil.isNull(request.getStaffName())){
            queryWrapper.like("staff_name",request.getStaffName());
        }
        if(!CommonUtil.isNull(request.getJobs())){
            queryWrapper.like("jobs",request.getJobs());
        }
        PageHelper.startPage(request.getPage(), request.getSize());
        List<EntityRestStaff>  foodstuffInfos = this.list(queryWrapper);
        PageInfo<EntityRestStaff> pageInfo=new PageInfo<>(foodstuffInfos);
        return pageInfo;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean removeRestStaff(Long companyId, RemoveRestStaffRequest request) {
        ScopeQueryWrapper<EntityRestStaff> queryWrapper = new ScopeQueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", request.getMerchId());
        queryWrapper.eq("staff_id", request.getStaffId());
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean newRestStaff(Long companyId, EntityRestStaff entityRestStaff) {
        return this.save(entityRestStaff);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean updateRestStaff(Long companyId, EntityRestStaff entityRestStaff) {
        return this.updateById(entityRestStaff);
    }
}
